/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.pools;

import gestionedt.controllers.PoolJpaController;
import gestionedt.models.Pool;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 * 
 */
public class SaveAction extends org.apache.struts.action.Action {
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if(isCancelled(request))
            return mapping.findForward("cancel");

        PoolsForm gForm = (PoolsForm) form;
        Pool pool = gForm.getPool();
        boolean nv = (pool.getId()==null || pool.getId() == 0);
        PoolJpaController ctPool = new PoolJpaController();
        if(nv)
            ctPool.create(pool);
        else {
            Pool p = ctPool.findPool(pool.getId());
            p.setLibelle(pool.getLibelle());
            p.setDescription(pool.getDescription());
            p.setNbPrestMaxParPlage(pool.getNbPrestMaxParPlage());
            ctPool.edit(pool);
        }

        return new ActionForward("/GererPools.do?action=afficher" +
                                         "&idPool="+pool.getId(), true);
    }
}
